﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcApplication6.Models;
using MvcApplication6.DAL;

namespace MvcApplication6.BIZ
{
    public class MenuL1BIZ
    {
        MenuL1DAL DAL = new MenuL1DAL();

		MenuL1DAL MenuL1DAL = new MenuL1DAL();
        MenuL2DAL MenuL2DAL = new MenuL2DAL();
        MenuL3DAL MenuL3DAL = new MenuL3DAL();

        ProductBIZ BIZ_product = new ProductBIZ();
		
		public List<MenuL1Model> GetAllMenu()
        {
            try
            {
                List<MenuL1Model> lst1 = new List<MenuL1Model>();
                List<MenuL2Model> lst2 = new List<MenuL2Model>();
                lst1 =  MenuL1DAL.Menu_l1Select();
                int index1 = 0;
                foreach (var itemMenuL1 in lst1)
                {

                    lst2 = MenuL2DAL.Menu_l2SearchById_l1(itemMenuL1.Id);
                    if (lst2 != null)
                    {
                        int index2 = 0;
                        foreach (var itemMenuL2 in lst2)
                        {
                            lst2[index2].MenuL3ModelList =  MenuL3DAL.Menu_l3SearchById_l2(itemMenuL2.Id);
                            index2++;
                        }

                        lst1[index1].MenuL2ModelList = lst2;
                        index1++;
                    }

                    

                }

                return lst1;
            }
            catch (Exception)
            {
                throw;
            }

        }
		
        public List<MenuL1Model> Menu_l1Select()
        {
            try
            {
                List<MenuL1Model> lst = new List<MenuL1Model>();
                lst = DAL.Menu_l1Select();

                return lst;
            }
            catch (Exception)
            {
                throw;
            }

        }

        //---------------------Menu_l1SearchById-----------------------------
        public MenuL1Model Menu_l1SearchById(int id)
        {
            try
            {
                MenuL1Model menul1 = DAL.Menu_l1SearchById(id);

                return menul1;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //---------------------Menu_l1Insert-----------------------------
        public int Menu_l1Insert(string name, int position)
        {
            try
            {
                int i = DAL.Menu_l1Insert(name, position);

                return i;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //---------------------Menu_l1Update-----------------------------
        public int Menu_l1Update(int id, string name, int position)
        {
            try
            {
                int i = DAL.Menu_l1Update(id, name, position);

                return i;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //---------------------Menu_l1Delete-----------------------------
        public int Menu_l1Delete(int id)
        {
            try
            {
                int i = DAL.Menu_l1Delete(id);

                return i;
            }
            catch (Exception)
            {
                throw;
            }
        }

        //----------------------Delete menu L1 voi dk co san pham
        public int DeleteMenu_L1WhenHadProduct(int idmenu)
        {
            List<ProductModel> lst_model = new List<ProductModel>();

            lst_model = BIZ_product.ProductSearchById_l1(idmenu);
            int so_luong = lst_model.Count();

            if (so_luong == 0)
            {
                int i = Menu_l1Delete(idmenu);
                return i;
            }

            return -1;
        }
    }
}